package com.cango.framework.base.converter;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cango.framework.base.util.NumberTools;
import com.cango.framework.base.util.NumberTools;
import org.apache.ibatis.type.DoubleTypeHandler;
import org.apache.ibatis.type.JdbcType;

/**
 * <p>Created by colick on 2015/5/22.</p>
 */
public class TwoDecimalDoubleTypeHander extends DoubleTypeHandler {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Double parameter, JdbcType jdbcType) throws SQLException {
        ps.setDouble(i, parameter);
    }

    @Override
    public Double getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return NumberTools.keepTwoDecimalDouble(rs.getDouble(columnName));
    }

    @Override
    public Double getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return NumberTools.keepTwoDecimalDouble(rs.getDouble(columnIndex));
    }

    @Override
    public Double getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return NumberTools.keepTwoDecimalDouble(cs.getDouble(columnIndex));
    }
}
